<?php
require_once '../utils/response.php';
require_once  '../utils/db.php';

header('content-type:text/html:charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pageSize = isset($_GET['pageSize']) ? $_GET['pageSize'] : 5;


// if (!is_numeric($page) || !is_numeric($pageSize)) {
//     return Response::show(401, '数据不合法');
// }

try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403, '数据库连接失败', $e->getMessage());
}

$offset = ($page - 1) * $pageSize;

$sql = "SELECT * FROM msg ORDER BY zwh_id DESC LIMIT {$offset},{$pageSize}";
$res = $connect->query($sql);

$data = array();

if ($res && $res->num_rows >= 0) {
    while ($info = $res->fetch_assoc()) {
        $data[] = $info;
    }
    $res->free_result();
    $sql = "SELECT COUNT(*) FROM msg";
    $res = $connect->query($sql);
    $num = $res->fetch_row();
    return Response::show(200, $num[0], $data);
} else {
    return Response::show(404, '获取留言信息失败');
}
